<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Attendance_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
	}
	
	/**
	* @package: Attendance details 
	* @author: pratik
*/

public function get_attendance()
{
	
}
 
  public function get_attendance_details()
  {	  
	  $user_id = array(
	  'user_id' => $this->session->userdata('user_id')
	  );
	  
 		return $query = $this->db->get_where('daily_attendance_sheet', $user_id)->result();
		
  }
	
	public function get_details($year_id,$month_id)
	{
		$serach = array(
		'year_id' =>$year_id,
		'month_id'=>$month_id,
		'user_id' => $this->session->userdata('user_id')		
		);
		
		return $this->db->get_where('daily_attendance_sheet',$serach)->result();
	}
	
	public function get_total_working_hr()
	{
	//	$this->db->select_avg('age');
		//$query = $this->db->get('members');		
	}
	
	public function get_time($year_id,$month_id,$date_id){
		
		$check = array('year_id' => $year_id, 'month_id' => $month_id, 'date_id' => $date_id,'user_id'=>$this->session->userdata('user_id'));			$this->db->select('entry_time, exit_time');
		$this->db->from('attendance_timings');
		$this->db->where($check);
		return $query = $this->db->get()->row_array();
	}
	
	public function get_working_hr()
	{
		$check = array('user_id'=>$this->session->userdata('user_id'));	
		$this->db->select('min_working_hour, per_day_salary ');
		$this->db->from('salary_account');
		$this->db->where($check);
		return $query = $this->db->get()->row_array();
	}
	
	function get_time_by_id($search){
		return $this->db->get_where('attendance_timings',$search)->row_array();
	}
	
	public function get_exit_time($search){
		//return $this->db->get_where('attendance_timings',$search)->row_array();
	}
	
	public function get_date_col_name()
	{
		return $this->db->list_fields('daily_attendance_sheet');;
	}
	
	function get_server_time()
	{
		$query = $this->db->query("select CURTIME()")->row_array();
		return $query['CURTIME()'];		
	}
	
	function get_server_date()
	{
		$query = $this->db->query("select CURDATE()")->row_array();
		return $query['CURDATE()'];		
	}
	
	function insert_entry_time($insert)
	{
		//echo "insert"; exit;
		$query = $this->db->get_where('attendance_timings', array(
		'year_id'=>$insert['year_id'],
		'month_id'=>$insert['month_id'],
		'date_id'=>$insert['date_id'],
		'user_id'=>$insert['user_id']
		));
 if($query->num_rows() == 0)
 { 
 $query = $this->db->insert('attendance_timings', $insert);		
		if($query)
		{			
		return "Inserted Successfully";	
		}
		else {
		return "Error in database. Please try Again";
		}
 }
 else {	return 0;  } 
	}
		
	/*fucntion to check entry time */
	public function check_entry_time($check)
	{
		$query = $this->db->get_where('attendance_timings', array(
		'month_id'=>$check['month_id'],
		'date_id'=>$check['date_id'],
		'user_id'=>$check['user_id']
		));
 if($query->num_rows() > 0){
	$row = $query->row_array();	
	return $row['entry_time'];
}
else{return false;}
}

public function insert_exit_time($time)
	{
	$check = array(
		 'month_id' => $time['month_id'],
		 'date_id' => $time['date_id'],
		 'user_id' => $time['user_id'],
			);
				 
	$data = array(
               'exit_time' => $time['exit_time'],
			   'exit_ip'=>$_SERVER['REMOTE_ADDR']            
            );
			
$query = $this->db->update('attendance_timings', $data, $check);	 
if($query)	{	
	// after exit time entered update the daily attendance sheets	
return "Attendance Updated Successfully";	
			}
else	{
return false;
		}
  }
  
 public function check_exit_time($check)
  {
	 $query = $this->db->get_where('attendance_timings', $check,1); 
	 if($query->num_rows() == 1)
	 {
		 $row = $query->row_array();
		 return $row['exit_time'];
	 }
	 else {
		return false; 
	 }
  }
 

 
  
  
  /**
* 
* new function attendance 
* @author pratik
* 
*/

public function get_daily_attendance()
{
	
}

   public function insert_daily_attendance_sheet($insert,$sunday)
  {
	  $check = array(
	  'year_id'  =>$insert['year_id'],
	  'month_id' =>	$insert['month_id'],
	  'user_id'  =>$insert['user_id'],	  
	  );
	
	 foreach($sunday as $sundays)
	 {	
		$insert_sunday['day_'.$sundays] = 'sunday';		
	 }
	
	$data = array(
	 array_pop(array_keys($insert))  => end(array_values($insert))
	 );	
	 
	// print_r($data);exit;'
	  $check_query = $this->db->get_where('daily_attendance_sheet', $check); 
	 if($check_query->num_rows() == 1)
	  {
	$query = $this->db->update('daily_attendance_sheet', $data, $check);	
	  }
	  else {	  
		$query = $this->db->insert('daily_attendance_sheet', $insert);    
	 $id = array(
	 'id' => $this->db->insert_id()
	 );
	 	 
	 //inserting all sundays
	 $this->db->update('daily_attendance_sheet', $insert_sunday, $id);	 
	 
	 //inserting all holidays
	 //$this->db->update('daily_attendance_sheet', $insert_holiday, $id);	 
	   
	   //insert into salary table
	   
	   $salary = $this->get_salary_by_id();		
		
	  //insert into monthly table
		$salary_insert= array(
		'year_id'=> $insert['year_id'],
		'month_id'=>$insert['month_id'],
		'user_id'=>$insert['user_id'],
		'salary_id' => $salary['id'],
		);
		
		
	  $query = $this->db->insert('monthly_salary_details', $salary_insert);  
	  }
		if($query)
		{			
		return "Inserted Successfully";	
		}
		else {
		return "Error in database. Please try Again";
		}
  }
  
  public function get_salary_by_id($id = null)
  {
	 $user_id = isset($id) ?  $id : $this->session->userdata('user_id');
	  $id = array(
	  'user_id' => $user_id
	  );
	return  $query = $this->db->get_where('salary_account',$id)->row_array();	 
  }
  
  public function add_to_monthly_salary()
  {
	
  }
  
  /**
  * update
  */
 
 public function insert_edited_time($where,$entry_time)
 {
 	
	 $insert = array(
	 	'year_id'   => $where['year_id'],
	 	'month_id'  => $where['month_id'],
	 	'date_id'   => $where['date_id'],
	 	'user_id'   => $where['user_id'],
	 	'entry_time'=> $entry_time,
	 	'entry_ip'  => $_SERVER['REMOTE_ADDR'],
	 );	 

	$this->db->insert('attendance_timings',$insert);
 	
 }
 
 public function update_entry_time($where,$entry_time)
 {
 	$this->db->update('attendance_timings',array('entry_time'=>$entry_time),$where);	
 }
 
 
  public function update_exit_time($where,$exit_time)
 {
 	//print_r($where);
	//exit;
 	$this->db->update('attendance_timings',array('exit_time'=>$exit_time),$where);
	
 }
 public function update_working_hr($src,$hrs)
 {
 	
	$new_search = array(
	'year_id' => $src['year_id'],
	'month_id' => $src['month_id'],
	'user_id' => $src['user_id'],
	);
//print_r($hrs);
 		//exit;
	$date_col = 'day_'.$src['date_id'];
 	$this->db->update('daily_attendance_sheet',array($date_col=>$hrs),$new_search);
 }
  
}
	


